package com.ssm.server.util;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * MD5加密工具类
 * @author lidongdong
 * 2017年12月5日 下午5:45:01
 */
public final class MD5Util {
	private static final char HEX_DIGITS[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
	
	private MD5Util(){
		
	}
	  
	private static String toHexString(byte[] b) {  
	    StringBuilder sb = new StringBuilder(b.length * 2);  
	    for (int i = 0; i < b.length; i++) {  
	        sb.append(HEX_DIGITS[(b[i] & 0xf0) >>> 4]);  
	        sb.append(HEX_DIGITS[b[i] & 0x0f]);  
	    }  
	    return sb.toString();  
	}  
	  
	public static String encrypt32(String SourceString) throws UnsupportedEncodingException {  
	    MessageDigest digest;
		try {
			digest = MessageDigest.getInstance("MD5");
			digest.update(SourceString.getBytes("UTF-8"));  
		    byte messageDigest[] = digest.digest();
		    return toHexString(messageDigest);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}  
	    return null;
	}  
	  
	public static String encrypt16(String SourceString) throws UnsupportedEncodingException {  
	    return encrypt32(SourceString).substring(8, 24);  
	}
	public static String MD5Encode(String origin, String charsetname) {    
        String resultString = null;    
        try {    
            resultString = new String(origin);    
            MessageDigest md = MessageDigest.getInstance("MD5");    
            if (charsetname == null || "".equals(charsetname))    
                resultString = byteArrayToHexString(md.digest(resultString    
                        .getBytes()));    
            else    
                resultString = byteArrayToHexString(md.digest(resultString    
                        .getBytes(charsetname)));    
        } catch (Exception exception) {    
        }    
        return resultString;    
    }    
	
	private static String byteArrayToHexString(byte b[]) {    
        StringBuffer resultSb = new StringBuffer();    
        for (int i = 0; i < b.length; i++)    
            resultSb.append(byteToHexString(b[i]));    
    
        return resultSb.toString();    
    }    
    
    private static String byteToHexString(byte b) {    
        int n = b;    
        if (n < 0)    
            n += 256;    
        int d1 = n / 16;    
        int d2 = n % 16;    
        return hexDigits[d1] + hexDigits[d2];    
    }    
    
    private static final String hexDigits[] = { "0", "1", "2", "3", "4", "5",    
            "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };  
}
